<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* RESOURCE class
*
*****/
class RESOURCE
{
// Constructor
	function RESOURCE($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/messages/SUCCESS.php");
		$this->success = new SUCCESS();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
	}
// If $noMenu, display Resources submenu links
	function display($noMenu = FALSE)
	{
		$this->template->setVar('heading', $this->messages->text("heading", "resources"));
		if($noMenu)
		{
			include_once("core/html/MENU.php");
			$menu = new MENU();
			$pString = $menu->noMenu('resource');
			$this->template->setVar('noMenu', $pString);
			return $this->template->process();
		}
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// check we are allowed to write
	function gateKeep($function, $message = FALSE)
	{
		if(!$this->session->getVar('setup_write'))
		{
			include_once("core/authorize/AUTHORIZE.php");
			$authorize = new AUTHORIZE($this->db);
			return $authorize->initLogon();
		}
// new resource
		if($function == 'newResource')
		{
			$this->session->setVar('resourceLock', FALSE);
			include_once("core/resource/RESOURCENEW.php");
			$newResource = new RESOURCENEW($this->db, $this->vars);
			return $newResource->resource($message);
		}
// edit resource
		if($function == 'editResource')
		{
			include("config.php");
			if($WIKINDX_ORIGINATOR_EDITONLY) // originator and admins only can edit
			{
				$resultSet = $this->db->select(array('WKX_resource_misc'), array('addUserIdResource'), 
					' WHERE ' . $this->db->formatField('id') . '=' . $this->db->tidyInput($this->vars['id']));
				if(!$this->session->getVar('setup_superadmin') && 
					($this->session->getVar('setup_userId') != $this->db->fetchOne($resultSet)))
				{
					include_once("core/authorize/AUTHORIZE.php");
					$authorize = new AUTHORIZE($this->db);
					return $authorize->initLogon();
				}
			}
			$this->session->setVar('resourceLock', FALSE);
			include_once("core/resource/RESOURCEEDIT.php");
			$editResource = new RESOURCEEDIT($this->db, $this->vars);
			return $editResource->resource();
		}
// write resource input
		if($function == 'writeResource')
		{
			if($this->session->getVar('resourceLock'))
			{
				include_once("core/html/CLOSE.php");
				include_once("core/messages/ERRORS.php");
				$this->errors = new ERRORS();
				$this->template->setVar('heading', $this->messages->text("heading", "resources"));
				$this->template->setVar('body', $this->errors->text("done", "resource"));
				new CLOSE($this->db, $this->template->process());
			}
			include_once("core/resource/RESOURCEWRITE.php");
			$writeResource = new RESOURCEWRITE($this->db, $this->vars);
			return $writeResource->writeResource();
		}
// edit resource keywords and categories
		if($function == 'editResourceKeywordCategory')
		{
			include("config.php");
			if($WIKINDX_ORIGINATOR_EDITONLY) // originator and admins only can edit
			{
				$resultSet = $this->db->select(array('WKX_resource_misc'), array('addUserIdResource'), 
					' WHERE ' . $this->db->formatField('id') . '=' . $this->db->tidyInput($this->vars['id']));
				if(!$this->session->getVar('setup_superadmin') && 
					($this->session->getVar('setup_userId') != $this->db->fetchOne($resultSet)))
				{
					include_once("core/authorize/AUTHORIZE.php");
					$authorize = new AUTHORIZE($this->db);
					return $authorize->initLogon();
				}
			}
			$this->session->setVar('resourceLock', FALSE);
			include_once("core/resource/RESOURCEKEYWORDCATEGORY.php");
			$editResource = new RESOURCEKEYWORDCATEGORY($this->db, $this->vars);
			return $editResource->editDisplay();
		}
// write resource keywords and categories
		if($function == 'writeResourceKeywordCategory')
		{
			if($this->session->getVar('resourceLock'))
			{
				include_once("core/html/CLOSE.php");
				include_once("core/messages/ERRORS.php");
				$this->errors = new ERRORS();
				$this->template->setVar('heading', $this->messages->text("heading", "resources"));
				$this->template->setVar('body', $this->errors->text("done", "resource"));
				new CLOSE($this->db, $this->template->process());
			}
			include_once("core/resource/RESOURCEKEYWORDCATEGORY.php");
			$editResource = new RESOURCEKEYWORDCATEGORY($this->db, $this->vars);
			return $editResource->edit();
		}
// edit/add note
		if($function == 'editNote')
		{
			include_once("core/resource/RESOURCENOTE.php");
			$obj = new RESOURCENOTE($this->db, $this->vars);
			if(!array_key_exists('noteText', $this->vars))
			{
				$this->session->setVar('resourceNoteLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// edit/add quote
		if($function == 'editQuote')
		{
			include_once("core/resource/RESOURCEQUOTE.php");
			$obj = new RESOURCEQUOTE($this->db, $this->vars);
			if(!array_key_exists('quoteText', $this->vars) && !array_key_exists('commentOnly', $this->vars))
			{
				$this->session->setVar('resourceQuoteLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// edit/add paraphrase
		if($function == 'editParaphrase')
		{
			include_once("core/resource/RESOURCEPARAPHRASE.php");
			$obj = new RESOURCEPARAPHRASE($this->db, $this->vars);
			if(!array_key_exists('paraphraseText', $this->vars) && !array_key_exists('commentOnly', $this->vars))
			{
				$this->session->setVar('resourceParaphraseLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// edit/add musing
		if($function == 'editMusing')
		{
			include_once("core/resource/RESOURCEMUSING.php");
			$obj = new RESOURCEMUSING($this->db, $this->vars);
			if(!array_key_exists('musingText', $this->vars))
			{
				$this->session->setVar('resourceMusingLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// edit/add abstract
		if($function == 'editAbstract')
		{
			include_once("core/resource/RESOURCEABSTRACT.php");
			$obj = new RESOURCEABSTRACT($this->db, $this->vars);
			if(!array_key_exists('abstractText', $this->vars))
			{
				$this->session->setVar('resourceAbstractLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// edit custom field
		if($function == 'editCustom')
		{
			include_once("core/resource/RESOURCECUSTOM.php");
			$obj = new RESOURCECUSTOM($this->db, $this->vars);
			if(!array_key_exists('customText', $this->vars))
			{
				$this->session->setVar('resourceCustomLock', FALSE);
				return $obj->editDisplay();
			}
			return $obj->edit();
		}
// add selected resources to user bibliographies, categories or keywords - display initial forms
		if($function == 'selectedResourceTo')
		{
			if(!array_key_exists('resourceSelectedTo', $this->vars))
				$this->failure($this->errors->text("inputError", "missing"));
			$this->session->setVar("resourceSelectedTo", $this->vars['resourceSelectedTo']);
			if($this->vars['resourceSelectedTo'] == 1)	// add to categories
				return $this->addToCategoryInit();
			else if($this->vars['resourceSelectedTo'] == 2)	// add to keywords
				return $this->addToKeywordInit();
			else if($this->vars['resourceSelectedTo'] == 3)	// remove resources from user bibliography
				return $this->deleteFromBib();
			else if($this->vars['resourceSelectedTo'] == 4)	// delete resources from WIKINDX
				return $this->deleteFromWikindx();
			else	// 0 -> add to user bibs
				return $this->addToUserBibInit();
		}
// add selected resources to categories
		if($function == 'addResourceToCategory')
			return $this->addToCategory();
// add selected resources to keywords
		if($function == 'addResourceToKeyword')
			return $this->addToKeyword();
	}
// write input to session
	function writeSession()
	{
// Write all input with 'resource_' prefix to session
		foreach($this->vars as $key => $value)
		{
			if(preg_match("/^resource_/", $key))
				$temp[$key] = trim($value);
		}
		if(isset($temp))
			$this->session->writeArray($temp);
	}
// Add selected resources to category(s)
	function addToCategory()
	{
		include_once("core/user/MYWIKINDX.php");
		$mywikindx = new MYWIKINDX($this->db, $this->vars);
		$addAll = $replaceExisting = FALSE;
		if(!array_key_exists("categories", $this->vars))
			return $mywikindx->chooseDisplay($this->errors->text("inputError", "missing"));
		$categoryIds = $this->vars['categories'];
		if(array_key_exists("replaceExisting", $this->vars))
			$replaceExisting = TRUE;
		if(array_key_exists("selectAll", $this->vars))
		{
			if(!$sql = stripslashes($this->session->getVar('sql_stmt')))
				$this->failure($this->errors->text("file", "noSql"));
			$recordset = $this->db->query($sql);
			while($row = $this->db->loopRecordSet($recordset))
				$resourceIds[] = $row['resourceId'];
		}
		else
		{
			foreach($this->vars as $key => $value)
			{
				if(strpos($key, "bib_") !== FALSE)
				{
					$split = explode("_", $key);
					$resourceIds[] = $split[1];
				}
			}
		}
		if(!isset($resourceIds))
			return $mywikindx->chooseDisplay($this->errors->text("inputError", "missing"));
		$this->session->setVar("resourceAddToCategories", join(",", $categoryIds));
		foreach($resourceIds as $resourceId)
		{
// Current keywords for this resource
			$fields = $values = array();
			$cIds = $categoryIds;
			$checkCategories = FALSE;
			$recordset = $this->db->select(array('WKX_resource_category'), array('categories'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($resourceId));
			if($this->db->numRows($recordset))
			{
				$oldRow = $this->db->fetchRow($recordset);
				$checkCategories = array_diff(explode(",", $oldRow['categories']), $categoryIds);
			}
			if(!$replaceExisting && is_array($checkCategories))
				$cIds = array_merge($checkCategories, $categoryIds);
			$fields[] = "categories";
			$values[] = implode(',', $cIds);
//			$recordset = $this->db->select(array('WKX_resource_keyword'), array('id'), 
//				" WHERE " . $this->db->formatField('id') . "=" . $resourceId);
			if($this->db->numRows($recordset))
			{
				$updateArray['categories'] = array_shift($values);
				$this->db->update('WKX_resource_category', $updateArray, 
					" WHERE " . $this->db->formatField('id') . "=" . $resourceId);
			}
			else
			{
				$fields[] = "id";
				$values[] = $resourceId;
				$this->db->insert('WKX_resource_category', $fields, $values);
			}
		}
		$this->session->setVar("addToKeywordCategory", TRUE);
		return $mywikindx->chooseDisplay($this->success->text("addResourceToCategory"));
	}
// Add selected resources to keyword(s)
	function addToKeyword()
	{
		include_once("core/keyword/KEYWORD.php");
		$keyword = new KEYWORD($this->db);
		include_once("core/user/MYWIKINDX.php");
		$mywikindx = new MYWIKINDX($this->db, $this->vars);
		$addAll = $replaceExisting = FALSE;
		if(!array_key_exists("keywords", $this->vars) && !trim($this->vars['keywordText']))
			return $mywikindx->chooseDisplay($this->errors->text("inputError", "missing"));
		$keywordIds = $this->vars['keywords'];
		if(($key = array_search(0, $keywordIds)) !== FALSE) // ignore
			unset($keywordIds[$key]);
		$keywordText = trim($this->vars['keywordText']);
		if(empty($keywordIds) && !$keywordText)
			return $mywikindx->chooseDisplay($this->errors->text("inputError", "missing"));
		if($keywordText)
		{
			if($id = $keyword->checkExists($keywordText))
			{
				if(array_search($id, $keywordIds) === FALSE)
					$keywordIds[] = $id;
			}
			else
			{
// given keyword doesn't exist so now write to db
				$fields[] = "keyword";
				$values[0] = $keywordText;
				$this->db->insert('WKX_keyword', $fields, $values);
				$keywordIds[] = $this->db->lastAutoId('WKX_keyword');
			}
		}
		if(array_key_exists("replaceExisting", $this->vars))
			$replaceExisting = TRUE;
		if(array_key_exists("selectAll", $this->vars))
		{
			if(!$sql = stripslashes($this->session->getVar('sql_stmt')))
				$this->failure($this->errors->text("file", "noSql"));
			$recordset = $this->db->query($sql);
			while($row = $this->db->loopRecordSet($recordset))
				$resourceIds[] = $row['resourceId'];
		}
		else
		{
			foreach($this->vars as $key => $value)
			{
				if(strpos($key, "bib_") !== FALSE)
				{
					$split = explode("_", $key);
					$resourceIds[] = $split[1];
				}
			}
		}
		if(!isset($resourceIds))
			return $mywikindx->chooseDisplay($this->errors->text("inputError", "missing"));
		$checkKIds = array();
		$this->session->setVar("resourceAddToKeywords", join(",", $keywordIds));
		foreach($resourceIds as $resourceId)
		{
// Current keywords for this resource
			$fields = $values = array();
			$kIds = $keywordIds;
			$checkKeywords = FALSE;
			$recordset = $this->db->select(array('WKX_resource_keyword'), array('keywords'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($resourceId));
			if($this->db->numRows($recordset))
			{
				$oldRow = $this->db->fetchRow($recordset);
				$checkKeywords = array_diff(explode(",", $oldRow['keywords']), $keywordIds);
			}
			if(!$replaceExisting && is_array($checkKeywords))
				$kIds = array_merge($checkKeywords, $keywordIds);
			else if(is_array($checkKeywords))
			{
				$checkKIds = array_merge($checkKIds, $checkKeywords);
				$checkKIds = array_unique($checkKIds);
			}
			$fields[] = "keywords";
			$values[] = implode(',', $kIds);
//			$recordset = $this->db->select(array('WKX_resource_keyword'), array('id'), 
//				" WHERE " . $this->db->formatField('id') . "=" . $resourceId);
			if($this->db->numRows($recordset))
			{
				$updateArray['keywords'] = array_shift($values);
				$this->db->update('WKX_resource_keyword', $updateArray, 
					" WHERE " . $this->db->formatField('id') . "=" . $resourceId);
			}
			else
			{
				$fields[] = "id";
				$values[] = $resourceId;
				$this->db->insert('WKX_resource_keyword', $fields, $values);
			}
		}
// remove keyword(s) if they no longer have resource attached to them
		if(!empty($checkKIds))
			$keyword->removeHanging2(join(",", $checkKIds));
		$this->session->setVar("addToKeywordCategory", TRUE);
		return $mywikindx->chooseDisplay($this->success->text("addResourceToKeyword"));
	}
// Add selected resources to categories - display form for this.
	function addToCategoryInit()
	{
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/TABLE.php");
		include_once("core/html/HTML.php");
		$html = new HTML();
		include_once("core/group/GROUP.php");
		$group = new GROUP($this->db);
		$categories = $group->grabAll();
		$this->template->setVar('heading', $this->messages->text("heading", "addToCategory"));
		$pString = FORM::formHeader("addResourceToCategory");
		$pString .= FORM::hidden("method", "add");
		if(array_key_exists("selectAll", $this->vars))
			$pString .= FORM::hidden("selectAll", "on");
		foreach($this->vars as $key => $value)
		{
			if(strstr($key, "bib_"))
				$pString .= FORM::hidden($key, "on");
		}
		$checkBox = $this->messages->text("resources", "addToExistingCategory") . "&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, "replaceExisting");
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		if($string = $this->session->getVar("resourceAddToCategories"))
		{
			$selectedArray = explode(",", $string);
			$pString .= TABLE::td(FORM::selectedBoxValueMultiple(FALSE, "categories", $categories, 
				$selectedArray, 10) . 
				MISC::br() . $this->messages->text("hint", "multiples") . 
				MISC::p($checkBox) . MISC::p(FORM::formSubmit("Add")));
		}
		else
			$pString .= TABLE::td(FORM::selectFBoxValueMultiple(FALSE, "categories", $categories, 10) . 
				MISC::br() . $this->messages->text("hint", "multiples") . 
				MISC::p($checkBox) . 
				MISC::p(FORM::formSubmit("Add")));
		$pString .= FORM::formEnd();
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Add selected resources to keywords - display form for this.
	function addToKeywordInit()
	{
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/TABLE.php");
		include_once("core/html/HTML.php");
		$html = new HTML();
		include_once("core/keyword/KEYWORD.php");
		$keyword = new KEYWORD($this->db);
		$keywords = $keyword->grabAll();
		$keywords = array(0 => $this->messages->text("misc", "ignore")) + $keywords;
		$this->template->setVar('heading', $this->messages->text("heading", "addToKeyword"));
		$pString = FORM::formHeader("addResourceToKeyword");
		$pString .= FORM::hidden("method", "add");
		if(array_key_exists("selectAll", $this->vars))
			$pString .= FORM::hidden("selectAll", "on");
		foreach($this->vars as $key => $value)
		{
			if(strstr($key, "bib_"))
				$pString .= FORM::hidden($key, "on");
		}
		$checkBox = $this->messages->text("resources", "addToExistingKeyword") . "&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, "replaceExisting");
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$pString .= TABLE::td(FORM::selectFBoxValueMultiple(FALSE, "keywords", $keywords, 10) . 
			MISC::br() . $this->messages->text("hint", "multiples"));
		$pString .= TABLE::td(FORM::textInput($this->messages->text("resources", "keyword"), 
			"keywordText", FALSE, 50) . MISC::br() . $this->messages->text('hint', 'keywordList'));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::p($checkBox) . MISC::p(FORM::formSubmit("Add"));
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// in multi user mode, handle the display of different bibliographies. 
// This is called from list, search and select process methods
	function addToUserBibInit()
	{
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
		include_once("core/html/TABLE.php");
		include_once("core/html/HTML.php");
		$html = new HTML();
		$usingBib = $pString = FALSE;
		$bibs = $tempU = $tempUG = array();
		$useBib = $this->session->getVar('mywikindx_bibliography_use');
		if($useBib)
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($useBib));
			$row = $this->db->fetchRow($recordset);
			$usingBib = $row['title'];
		}
		if($this->session->getVar('mywikindx_bibliographies'))
		{
			$bibsRaw = unserialize($this->session->getVar("mywikindx_bibliographies"));
// remove currently in use bibliography - if we're browsing it, all resources shown are already part of that bibliography
// if mywikindx_bibliography_use is not in mywikindx_bibliographies, this is another user's bibliography we are browsing - 
// if so, do not remove it as we might like to add to our own bibliography from it.
			if($useBib)
			{
				if(array_key_exists($useBib, $bibsRaw))
				{
					$usingBib = $bibsRaw[$useBib];
					unset($bibsRaw[$useBib]);
				}
			}
			foreach($bibsRaw as $key => $value)
				$tempU[$key] = $html->dbToFormTidy($value);
		}
// User group bibs of which this user is a member (therefore, has write access to)
		$userId = $this->session->getVar('setup_userId');
		$bibsRaw = array();
		$recordset = $this->db->select(array('WKX_user_groups'), 
			array(array('WKX_user_bibliography.id' => 'bibId'), 
				array('WKX_user_bibliography.title' => 'bibTitle')), 
			"LEFT JOIN " . $this->db->formatTable('WKX_user_bibliography') . 
				" ON FIND_IN_SET(" . $this->db->formatField('WKX_user_bibliography.id') . ", CONCAT(" . 
				$this->db->formatfield('bibliographyIds') . ", ','))" . 
			" WHERE " . 
			" FIND_IN_SET(" . $this->db->tidyInput($userId) . ", CONCAT(" . 
				$this->db->formatfield('userIds') . ", ','))" . 
			' ORDER BY ' . $this->db->tidyInputClause('WKX_user_bibliography.title'));
		while($row = $this->db->loopRecordSet($recordset))
		{
			if($row['bibId'])
				$bibsRaw[$row['bibId']] = $row['bibTitle'];
		}
// remove currently in use bibliography - if we're browsing it, all resources shown are already part of that bibliography
// if mywikindx_bibliography_use is not in mywikindx_bibliographies, this is another user's bibliography we are browsing - 
// if so, do not remove it as we might like to add to our own bibliography from it.
		if($useBib)
		{
			if(array_key_exists($useBib, $bibsRaw))
			{
				$usingBib = $bibsRaw[$useBib];
				unset($bibsRaw[$useBib]);
			}
		}
		foreach($bibsRaw as $key => $value)
			$tempUG[$key] = $html->dbToFormTidy($value);
		if(!empty($tempU))
		{
			$bibs[0] = $this->messages->text('user', 'userBibs');
			foreach($tempU as $key => $value)
				$bibs[$key] = $value;
		}
		if(!empty($tempUG))
		{
			$bibs[-1] = $this->messages->text('user', 'userGroupBibs');
			foreach($tempUG as $key => $value)
				$bibs[$key] = $value;
		}
		if(!empty($bibs))
		{
			include_once("core/html/HTML.php");
			$html = new HTML();
			$this->template->setVar('heading', $this->messages->text("heading", "addToBib"));
			$pString = FORM::formHeader("myWikindxBibliography");
			$pString .= FORM::hidden("method", "add");
			if(array_key_exists("selectAll", $this->vars))
				$pString .= FORM::hidden("selectAll", "on");
			foreach($this->vars as $key => $value)
			{
				if(strstr($key, "bib_"))
					$pString .= FORM::hidden($key, "on");
			}
			$pString .= TABLE::tableStart();
			$pString .= TABLE::trStart();
			$sessVar = $this->session->getVar("mywikindx_bibliography_add");
			if($sessVar)
				$pString .= TABLE::td(FORM::selectedBoxValue(FALSE, "bibId", $bibs, $sessVar, 10) . 
					MISC::p(FORM::formSubmit("Add")));
			else
				$pString .= TABLE::td(FORM::selectFBoxValue(FALSE, 
					"bibId", $bibs, 10) . MISC::p(FORM::formSubmit("Add")));
			$pString .= TABLE::trEnd();
			$pString .= TABLE::tableEnd();
		}
		$pString .= FORM::formEnd();
		$this->template->setVar('body', $pString);
		$this->session->setVar("addToKeywordCategory", TRUE);
		return $this->template->process();
	}
// Delete resources from a user bibliogaphy
	function deleteFromBib()
	{
		if(array_key_exists('selectAll', $this->vars))
			$inputBibs = unserialize(base64_decode($this->session->getVar('list_allIds')));
		else
		{
			foreach($this->vars as $key => $value)
			{
				if(preg_match("/bib_(.*)/", $key, $matches))
					$inputBibs[] = $matches[1];
			}
		}
		if(!isset($inputBibs))
			$this->failure($this->errors->text("inputError", "invalid"));
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
			" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->session->getVar('mywikindx_bibliography_use')));
// bibliography is a comma-separated list of unique resource ids.
		$dbBibs = $this->db->fetchOne($recordset);
		$dbBibs = explode(",", $dbBibs);
		$dbBibs = array_diff($dbBibs, $inputBibs);
		if(empty($dbBibs))
			$this->db->updateNull('WKX_user_bibliography', array('bibliography'),
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->session->getVar('mywikindx_bibliography_use')));
		else
		{
			$updateArray = array('bibliography' => implode(",", $dbBibs));
			$this->db->update('WKX_user_bibliography', $updateArray, 
				" WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($this->session->getVar('mywikindx_bibliography_use')));
		}
		$recordset = $this->db->select(array('WKX_user_bibliography'), array('id'), 
			" WHERE " . $this->db->formatField('bibliography') . ' IS NOT NULL');
		if($this->db->numRows($recordset))
			$this->session->setVar("setup_bibliographies", TRUE);
		else
			$this->session->delVar("setup_bibliographies");
		$this->session->setVar("addToKeywordCategory", TRUE);
		include_once("core/user/MYWIKINDX.php");
		$mywikindx = new MYWIKINDX($this->db, $this->vars);
		return $mywikindx->chooseDisplay($this->success->text("deleteFromBib"));
	}
// Delete resources from WIKINDX
	function deleteFromWikindx()
	{
		if(array_key_exists('selectAll', $this->vars))
			$inputIds = unserialize(base64_decode($this->session->getVar('list_allIds')));
		else
		{
			foreach($this->vars as $key => $value)
			{
				if(preg_match("/bib_(.*)/", $key, $matches))
					$inputIds[] = $matches[1];
			}
		}
		if(!isset($inputIds))
			$this->failure($this->errors->text("inputError", "invalid"));
		$this->vars['resource_id'] = $inputIds;
		$this->session->setVar("addToKeywordCategory", TRUE);
		include_once("core/admin/DELETERESOURCE.php");
		$deleteResource = new DELETERESOURCE($this->db, $this->vars);
		return $deleteResource->gateKeep('confirm');
	}
// failure - bomb out
	function failure($error)
	{
		$this->template->setVar("heading", $this->messages->text("heading", "resources"));
		include_once("core/html/CLOSE.php");
		$this->template->setVar('body', $error);
		new CLOSE($this->db, $this->template->process());
	}
}
?>
